package com.turing.post.organization.impl;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.organization.util.Voucher;
import com.turing.post.organization.util.VoucherDao;
import com.turing.post.report.util.BinaryFileInfoDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author wzd
 * @2013-03-12
 */
@Stateless(mappedName = "organization.impl.VoucherDaoImpl")
@Remote
@Local
public class VoucherDaoImpl implements VoucherDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@EJB(mappedName = "report.impl.BinaryFileInfoDaoImpl")
	private BinaryFileInfoDao bfiDaoImpl;

	/**
	 * 获取主键id（凭证ID）最大值
	 * 
	 * @return
	 */
	@Override
	public BigInteger getMaxIdVoucherId() {
		Query query = _em
				.createNativeQuery("select nvl(max(t.VoucherId),0)+1 IdTypeId from Voucher t");
		return new BigInteger(String.valueOf(((BigDecimal) query
				.getSingleResult())));
	}

	/**
	 * 根据凭证名称查询凭证种类列表---注：模糊查询
	 * 
	 * @param voucherName
	 *            凭证名称
	 * @param page
	 *            页码
	 * @return 凭证种类列表
	 */
	@SuppressWarnings("unchecked")
	public List<Voucher> getVoucherListByVoucherName(PCPSWebInput webInput,
			String voucherName, Integer page) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo()))
			return null;
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "select * from Voucher t where 1=1";
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
			sql = sql + " and t.IssueCompanyNo = '" + issueCompanyNo + "'";
		}
		if (!PCPSUtil.isEmpty(voucherName)) {
			sql = sql + " and t.voucherName like '%" + voucherName + "%'";
		}
		sql = sql + "order by VoucherId desc";
		Query query = _em.createNativeQuery(sql, Voucher.class).setFirstResult(
				startPosition).setMaxResults(maxResult);
		return query.getResultList();
	}

	/**
	 * 查询销售商记录条数
	 * 
	 * @param branchNo
	 *            所属机构
	 * @return BigInteger 记录总数
	 */
	@Override
	public BigInteger getVoucherListByVoucherNameTotal(PCPSWebInput webInput,
			String voucherName) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo()))
			return null;
		String sql = "select nvl(count(*),0)  from Voucher t where 1=1";
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
			sql = sql + " and t.IssueCompanyNo = '" + issueCompanyNo + "'";
		}
		if (!PCPSUtil.isEmpty(voucherName)) {
			sql = sql + " and t.voucherName like '%" + voucherName + "%'";
		}
		sql = sql + "order by VoucherId desc";
		Query query = _em.createNativeQuery(sql);
		return new BigInteger(query.getSingleResult().toString());
	}

	/**
	 * 根据凭证id查询凭证种类对象
	 * 
	 * @param voucherId
	 *            凭证Id
	 * @return 凭证种类对象
	 */
	@Override
	public Voucher getVoucherByVoucherId(BigInteger voucherId) {
		return _em.find(Voucher.class, voucherId);
	}

	/**
	 * 新增凭证种类
	 * 
	 * @param Voucher
	 *            凭证种类
	 * @return
	 * @throws PCPSApplicationException
	 */
	public void insertVoucher(Voucher voucher) throws PCPSApplicationException {
		GeneralDao.insert(_em, voucher);
	}

	/**
	 * 修改凭证种类
	 * 
	 * @param Voucher
	 *            凭证种类
	 * @throws PCPSApplicationException
	 */
	public void updateVoucher(Voucher voucher) throws PCPSApplicationException {
		GeneralDao.update(_em, voucher);
	}

	/**
	 * 根据凭证名称查询凭证种类列表---注：模糊查询(用于下拉或弹出)
	 * 
	 * @param voucherName
	 *            凭证名称
	 * @return 凭证种类列表
	 */
	@SuppressWarnings("unchecked")
	public List<Voucher> getVoucherDropListByVoucherName(PCPSWebInput webInput,
			String voucherName) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo()))
			return null;
		String sql = "select * from Voucher t where 1=1";
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
			sql = sql + " and t.IssueCompanyNo = '" + issueCompanyNo + "'";
		}
		if (!PCPSUtil.isEmpty(voucherName)) {
			sql = sql + " and t.voucherName like '%" + voucherName + "%'";
		}
		sql = sql + "order by VoucherId desc";
		Query query = _em.createNativeQuery(sql, Voucher.class);
		return query.getResultList();
	}

	/**
	 * @删除凭证种类
	 * @param voucherName
	 *            凭证名称
	 * @throws PCPSApplicationException
	 */
	@Override
	public void deleteVoucher(BigInteger voucherId)
			throws PCPSApplicationException {
		Voucher eo = this.getVoucherByVoucherId(voucherId);
		if (eo.getImageId() != null) {
			bfiDaoImpl.deleteBinaryFileInfoById(eo.getImageId());
		}
		if (eo != null) {
			GeneralDao.remove(_em, eo);
		}
	}

	/**
	 * 根据凭证种类名称检索凭证对象
	 * 
	 * @param voucherName
	 *            凭证名称
	 * @throws PCPSApplicationException
	 */
	@SuppressWarnings("unchecked")
	public List<Voucher> getVoucherByVoucherName(String issuecompanyno,
			String voucherName) {
		if (PCPSUtil.isEmpty(voucherName) || PCPSUtil.isEmpty(issuecompanyno))
			return null;
		String sql = "select * from Voucher where VoucherName='" + voucherName
				+ "' and IssueCompanyNo='" + issuecompanyno + "'";
		Query query = _em.createNativeQuery(sql, Voucher.class);
		return (List<Voucher>) query.getResultList();
	}

	/**
	 * 查询某发卡商的所有凭证种类
	 * 
	 * @param issueCompanyNo
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Voucher> getAllVoucherByIssueCompanyNo(String issueCompanyNo) {
		String sql = "select * from Voucher where IssueCompanyNo = ?1";
		Query query = _em.createNativeQuery(sql, Voucher.class);
		query.setParameter(1, issueCompanyNo);
		return (List<Voucher>) query.getResultList();
	}
}
